package zhoufu.sz.sanxiao_check.ui.check;

import android.databinding.ViewDataBinding;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import org.byteam.superadapter.list.BaseSuperAdapter;
import org.byteam.superadapter.list.BaseViewHolder;

import java.util.ArrayList;

import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityCheckHistoryListBinding;
import zhoufu.sz.sanxiao_check.inject.modules.PresenterModule;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.basic.BaseMsgActivity;

public class CheckHistoryListActivity extends BaseMsgActivity {
    ActivityCheckHistoryListBinding _binding;
    private BasicRequestRepository basicRequestRepository;

    @Override
    protected void initInject() {
        basicRequestRepository = new PresenterModule().provideBasicRequestRepository(this, new Gson());
        basicRequestRepository
                .GetCheckHistory(getIntent().getStringExtra("SID"), new RequestHelper.RequestCallBack<Object>() {
                    @Override
                    public void onPullStart() {

                    }

                    @Override
                    public void onPullFailud(Throwable throwable) {

                    }

                    @Override
                    public void onPullEnd() {

                    }

                    @Override
                    public void onPullSuccessed(Object s) {
                        LinkedTreeMap<String,Object> map = (LinkedTreeMap<String, Object>) s;
                        setData2ListView((ArrayList<LinkedTreeMap<String,String>>)map.get("data"));

                    }
                });
    }

    private void setData2ListView(ArrayList<LinkedTreeMap<String,String>> datas) {
        _binding.listview.setAdapter(new BaseSuperAdapter<LinkedTreeMap<String,String>,BaseViewHolder>(this,datas,R.layout.item_check_history) {
            @Override
            protected BaseViewHolder onCreate(int viewType, View convertView, ViewGroup parent) {
                return BaseViewHolder.get(getApplicationContext(),convertView,parent,R.layout.item_check_history);
            }

            @Override
            protected void onBind(int viewType, BaseViewHolder holder, int position, final LinkedTreeMap<String,String> item) {
                holder.setText(R.id.tv_check_date,"检查时间："+item.get("AddTime"));
                holder.setText(R.id.tv_check_person,"检查人："+item.get("Name"));
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showProgreesDialog();
                        basicRequestRepository.GetExamineForPrint(getIntent().getStringExtra("SID"), item.get("Id"), new RequestHelper.RequestCallBack<CheckItemModel>() {
                            @Override
                            public void onPullStart() {

                            }

                            @Override
                            public void onPullFailud(Throwable throwable) {
                                Toast.makeText(getApplicationContext(),"网络异常",Toast.LENGTH_SHORT).show();
                                hideProgreesDialog();
                            }

                            @Override
                            public void onPullEnd() {

                            }

                            @Override
                            public void onPullSuccessed(CheckItemModel s) {
                                hideProgreesDialog();
                                if ( s.isSuccess() )
                                {
                                    if ( s.getData().size() > 0 )
                                    {
                                        PreHtmlViewActivity.startAct(CheckHistoryListActivity.this,s.getData().get(0));
                                    }
                                } else
                                {
                                    Toast.makeText(getApplicationContext(),"没有找到检查记录",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
            }
        });
    }



    @Override
    protected void initAfterOnCreate(ViewDataBinding binding) {
        super.initAfterOnCreate(binding);
        setTitle("巡查记录");
        _binding = (ActivityCheckHistoryListBinding) binding;
    }

    @Override
    protected int setIndividualId() {
        return R.layout.activity_check_history_list;
    }

}
